package com.bw.zuoye0722.login

import android.annotation.SuppressLint
import android.app.Notification
import android.app.NotificationChannel
import android.app.NotificationManager
import android.graphics.Color
import android.os.Bundle
import android.provider.CalendarContract.Colors
import android.text.SpannableStringBuilder
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.text.style.ForegroundColorSpan
import android.view.LayoutInflater
import android.view.View
import android.widget.Button
import android.widget.Toast
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.launcher.ARouter
import com.blankj.utilcode.util.RegexUtils
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.bw.base.mvvm.BaseActivity
import com.bw.zuoye0722.R
import com.bw.zuoye0722.databinding.ActivityLoginBinding
import kotlinx.coroutines.delay
import kotlinx.coroutines.launch

@Route(path = "/app/login/LoginActivity")
class LoginActivity : BaseActivity<ActivityLoginBinding,LoginViewModel>() {
    override fun initView() {
        //富文本
        var fwb = binding.fwb.text.toString()
        var spannableStringBuilder = SpannableStringBuilder(fwb)
        spannableStringBuilder.setSpan(ForegroundColorSpan(Color.RED),12,18,SpannableStringBuilder.SPAN_INTERMEDIATE)
        var clickableSpan1=object :ClickableSpan(){
            override fun onClick(widget: View) {
                //弹出对话框
                var view = LayoutInflater.from(this@LoginActivity).inflate(R.layout.dhk, null)
                var no=view.findViewById<Button>(R.id.no)
                var yes=view.findViewById<Button>(R.id.yes)

                var dialog = AlertDialog.Builder(this@LoginActivity)
                    .setView(view)
                    .show()
                yes.setOnClickListener {
                    binding.cb.isChecked=true
                    dialog.dismiss()
                }
                no.setOnClickListener {
                    binding.cb.isChecked=false
                    dialog.dismiss()
                }
            }
        }
        spannableStringBuilder.setSpan(clickableSpan1,6,12,SpannableStringBuilder.SPAN_INTERMEDIATE)
        binding.fwb.movementMethod=LinkMovementMethod.getInstance()
        binding.fwb.text=spannableStringBuilder

        //点击按钮，获取验证码
        binding.getYzm.setOnClickListener {
            var tel=binding.etPhone.text.toString()
            if(tel.isEmpty()){
                ToastUtils.showLong("请先输入手机号码")
            }else{
                if(RegexUtils.isMobileExact(tel)){
                    if(binding.cb.isChecked){
                        viewModel.sendCode(tel)
                    }else{
                        ToastUtils.showLong("请先勾选协议")
                    }
                }else{
                    ToastUtils.showLong("手机号码格式错误")
                }
            }
        }
        binding.back.setOnClickListener {
            finish()
        }
    }

    @SuppressLint("NewApi", "NotificationPermission")
    override fun initData() {
        viewModel.sendCodeSuccess.observe(this){
            if(it.code==200){
                //发送通知
                var manager=getSystemService(NOTIFICATION_SERVICE) as NotificationManager
                manager.createNotificationChannel(NotificationChannel("ch1","通道1",NotificationManager.IMPORTANCE_DEFAULT))
                var notification = Notification.Builder(this,"ch1")
                    .setSmallIcon(R.mipmap.ic_launcher)
                    .setContentTitle("通知")
                    .setContentText("${it.data.content.substring(8, 14)}")
                    .build()
                manager.notify(1,notification)
                //倒计时
                lifecycleScope.launch {
                    binding.getYzm.isEnabled=false
                    for (i in 20 downTo 1) {
                        binding.getYzm.text="${i}s后重新获取"
                        if(i==19){
                            ARouter.getInstance().build("/app/CodeActivity")
                                .withString("phone",binding.etPhone.text.toString())
                                .navigation()
                        }
                        delay(1000)
                    }
                    binding.getYzm.text="获取验证码"
                    binding.getYzm.isEnabled=true
                }
            }
        }
    }

    override fun getViewModel(): Class<LoginViewModel> {
        return LoginViewModel::class.java
    }

    override fun getLayoutId(): Int {
        return R.layout.activity_login
    }
}